package gemini.castor.ui.client.page.content.customer.careschedule.form;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class CareScheduleFormView extends Composite implements
		CareScheduleFormPresenter.Display {

	@UiTemplate("CareScheduleFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, CareScheduleFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public CareScheduleFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}

	@UiField
	FlexTable ftbEvent;
	
	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void loadData() {

		// TODO: NHS-Init data for order table
		initOrderList();
		initOrderTable();
		ftbEvent.removeAllRows();
		initOrderHeader();
		RowFormatter rowFmt = ftbEvent.getRowFormatter();
		int i = 1;
		for (String[] it : orderList) {
			if (i % 2 == 0) {
				rowFmt.setStyleName(i, "rowEven");
			}
			for (int j = 0; j < 7; j++) {
				ftbEvent.setHTML(i, j, it[j]);
			}
			i++;
		}

	}

	private void initOrderTable() {
		ColumnFormatter colFmt = ftbEvent.getColumnFormatter();

		colFmt.setWidth(0, "7%");
		colFmt.setWidth(1, "30%");
		colFmt.setWidth(2, "12%");
		colFmt.setWidth(3, "12%");
		colFmt.setWidth(4, "12%");
		colFmt.setWidth(5, "12%");
		colFmt.setWidth(6, "15%");
	}

	private void initOrderHeader() {
		ftbEvent.setHTML(0, 0, HtmlUtils.bold(constants.htmOrderNumber()));
		ftbEvent.setHTML(0, 1, HtmlUtils.bold(constants.htmEventName()));
		ftbEvent.setHTML(0, 2, HtmlUtils.bold(constants.htmEventDate()));
		ftbEvent.setHTML(0, 3, HtmlUtils.bold(constants.htmType()));
		ftbEvent.setHTML(0, 4, HtmlUtils.bold(constants.htmLocation()));
		ftbEvent.setHTML(0, 5, HtmlUtils.bold(constants.htmDisplayTo()));
		ftbEvent.setHTML(0, 6, HtmlUtils.bold(constants.htmLastUpdate()));
		ftbEvent.getRowFormatter().setStyleName(0, "tableHeader");
		ftbEvent.setStyleName("ftbSummary");
	}

	private List<String[]> orderList = new ArrayList<String[]>();

	private void initOrderList() {
		orderList.clear();
		String[] st1 = { "01", "IL, Chicago - STS", "Feb 19",
				"Success Training Seminars", "North America", "All Distributors", "Jan 27" };
		orderList.add(st1);
		String[] st2 = { "02", "2011 Catch the Wave STS Promo Call", "Mar 07",
				"Conference call", "North America", "All Distributors", "Mar 05" };
		orderList.add(st2);
		String[] st3 = { "03", "2011 Nutrition Club Training Calls", "Jul 20",
				"Conference call", "North America", "All Distributors", "Feb 02" };
		orderList.add(st3);
	}

}
